# -*- coding: utf-8 -*-

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyKDE4.plasma import Plasma
from PyKDE4.kio import KRun
from PyKDE4.kdecore import KUrl 

class CoverWidget(Plasma.Label):

	perspectiveWidth = 125
	perspectiveScale = 385
	visiblo = 0
	hovering = False

	def __init__(self, parent):

		Plasma.Label.__init__(self, parent)

		self.setAcceptedMouseButtons(Qt.LeftButton)

		self.tracks = list()
		self.perspectiveMatrix = QTransform(0.2, -0.05, -0.001, 0, 0.3, 0.0, 0, 0, 1.0)

	def hoverMoveEvent(self, event):

		self.hovering = True

		length = len(self.tracks) - 1

		if length < 1:

			return

		#print 'mouse move:', event.pos().x(), 'x', event.pos().y()

		x = event.pos().x()
		offset = self.geometry().width() - self.perspectiveWidth;

		if x < self.perspectiveWidth:

			if self.visiblo != 0:

				self.visiblo = 0
				#print 'enter 0'
				self.update()
		else:

			step = offset / length

			visiblo = int((x - self.perspectiveWidth) / step) + 1

			if self.visiblo != visiblo:

				self.visiblo = visiblo
				#print 'enter', visiblo
				self.update()

	def hoverEnterEvent(self, event):

		self.hovering = True
		self.update()

	def hoverLeaveEvent(self, event):

		self.hovering = False

		if self.visiblo != 0:

			self.visiblo = 0

		self.update()

	def mousePressEvent(self, event):

		#print 'mouse press'

		self.emit(SIGNAL('clicked()'))

	def paint(self, painter, option, widget):

		length = len(self.tracks) - 1

		if length < 0:
			
			return

		offset = self.geometry().width() - self.perspectiveWidth;

		if length == 0:

			pixmap = self.tracks[0].pixmap
			scaledPixmap = pixmap.scaled(self.perspectiveScale, self.perspectiveScale)
			transformedPixmap = scaledPixmap.transformed(self.perspectiveMatrix, Qt.SmoothTransformation)
			painter.drawPixmap(offset / 2, 0, transformedPixmap)
		
		else:

			counter = 0
			step = offset / length

			for track in reversed(self.tracks):

				if (length - counter < self.visiblo):
					
					painter.setOpacity(0.1)
				else:

					painter.setOpacity(1.0)

				if track.pixmap == None:

					pixmap = self.drawDummyPixmap(track)
					track.pixmap = pixmap
				else:

					pixmap = track.pixmap

				scaledPixmap = pixmap.scaled(self.perspectiveScale, self.perspectiveScale, Qt.IgnoreAspectRatio, Qt.SmoothTransformation)
				transformedPixmap = scaledPixmap.transformed(self.perspectiveMatrix, Qt.SmoothTransformation)
				painter.drawPixmap(offset - counter * step, 0, transformedPixmap)
				counter += 1

		selectedTrack = self.tracks[self.visiblo]

		if self.hovering:

			painter.setOpacity(1.0)
			painter.setFont(QFont('Helvetica', 10));
			painter.setPen(QPen(Qt.black, 1));
			geometry = self.geometry()
			rect = QRectF(0, geometry.height() - 40, geometry.width(), 40)
			painter.drawText(rect, Qt.AlignLeft, selectedTrack.artist+"\n"+selectedTrack.title);

	# helper method

	def drawDummyPixmap(self, track):

		width = self.perspectiveScale
		height = width

		pixmap = QPixmap(width, height)
		painter = QPainter(pixmap)
		painter.setRenderHint(QPainter.TextAntialiasing, True)

		gradient = QLinearGradient(0, 0, width, height)
		gradient.setColorAt(0, Qt.white)
		gradient.setColorAt(1, Qt.lightGray)

		painter.fillRect(0, 0, width, height, gradient)

		painter.setFont(QFont('Helvetica', 25));
		painter.setPen(QPen(Qt.black, 1));
		rect = QRectF(10, 10, width - 20, height - 20)
		
		painter.drawText(rect, Qt.AlignRight, track.artist+"\n"+track.title);

		return pixmap

class ControlWidget(Plasma.Label):

	def __init__(self, parent):
	
		Plasma.Label.__init__(self, parent)

		self.playStop = Plasma.PushButton(self)
		self.playStop.setText('play')
		self.skip = Plasma.PushButton(self)
		self.skip.setText('skip')
		self.skip.setEnabled(False)
		self.love = Plasma.PushButton(self)
		self.love.setText('love')
		self.love.setEnabled(False)
		self.ban = Plasma.PushButton(self)
		self.ban.setText('ban')
		self.ban.setEnabled(False)
		self.layout = QGraphicsGridLayout(self)
		self.layout.addItem(self.playStop, 0, 0)
		self.layout.addItem(self.skip, 0, 1)
		self.layout.addItem(self.love, 1, 0)
		self.layout.addItem(self.ban, 1, 1)
		self.setLayout(self.layout)

	def hoverLeaveEvent(self, event):

		self.emit(SIGNAL('hoverEnd()'))

class SessionWidget(Plasma.Label):

	def __init__(self, parent):
	
		Plasma.Label.__init__(self, parent)
		
		self.ok = Plasma.PushButton(self)
		self.ok.setText('Ok')
		self.label = Plasma.Label(self)
		self.connect(self.label, SIGNAL('linkActivated(QString)'), self.openLinkInBrowser)
		layout = QGraphicsLinearLayout(Qt.Vertical, self)
		layout.addItem(self.label)
		layout.addItem(self.ok)
		self.setLayout(layout)

	def setLink(self, link):

		message = 'Visit <a href="' + link + '">this link</a> and allow streaming music<br/> to the plasmoid. Hit Ok <b>afterwards</b>.'
		self.label.setText(message)

	def openLinkInBrowser(self, link):

		KRun.runUrl(KUrl(link), 'text/html', None)